<template>
  <div >
    <MyHeader>
      <div slot="nav"></div>
    </MyHeader>
    <div class="store-content page-cart">
      <div class="gray-box">
        <div class="title"><h2>购物清单</h2></div>
        <!--内容-->
        <div v-if="cartList.length">
          <div class="ui-cart">
            <div>
              <!--标题-->
              <div class="cart-table-title">
                <span class="name">商品信息</span>
                <span class="operation">操作</span>
                <span class="subtotal">小计</span>
                <span class="num">数量</span>
                <span class="price">单价</span>
              </div>
              <!--列表-->
              <el-table :data="cartList" :show-header="false">
                <el-table-column width="30px" align="center"> 
                  <template slot-scope="scope">
                    <el-checkbox v-model="scope.row.selected" @change="stateChange(scope.row)" :disabled="scope.row.goods_number > scope.row.goods_info.goods_num"></el-checkbox>
                  </template>
                </el-table-column >
                <el-table-column width="641px">
                  <template slot-scope="scope">
                    <a @click="productDetail(scope.row.goods_info.id)" target="_blank">
                      <div class="items-thumb fl">
                        <img :alt="scope.row.goods_info.goods_name"
                              :src="'http://127.0.0.1:8000/' + scope.row.goods_info.goods_Bigimg" :title="scope.row.goods_info.goods_name">
                      </div>
                      <!--信息-->
                      <div class="name hide-row fl">
                        <div class="name-table">
                          <a @click="productDetail(scope.row.goods_info.id)" target="_blank" v-text="scope.row.goods_info.goods_name"></a>
                        </div>
                      </div>
                    </a>
                  </template>
                </el-table-column>
                <el-table-column width="137px" align="center">
                  <template slot-scope="scope">
                    {{ scope.row.goods_info.goods_price }}
                  </template>
                </el-table-column>
                <el-table-column width="145px" >
                  <template slot-scope="scope">
                    <BuyNum @handleValue="handleVal($event, scope.row)" :size="mini" :num="scope.row.goods_number"></BuyNum>
                  </template>
                </el-table-column>
                <el-table-column width="118px" align="center">
                  <template slot-scope="scope">
                    {{ scope.row.goods_number * scope.row.goods_info.goods_price }}
                  </template>
                </el-table-column>
                <el-table-column  align="center">
                  <template slot-scope="scope">
                    <el-button type="danger" icon="el-icon-delete" circle @click="delGoods(scope.row.id)"></el-button>
                  </template>
                </el-table-column>
              </el-table>
            </div>
          </div>
          <div class="cart-bottom-bg fix-bottom">
            <div class="fix-bottom-inner">
              <div class="cart-bar-operation">
                <div>
                  <div class="choose-all">
                    <el-checkbox v-model="isFull" :disabled="disable" style="margin-left: -16px;margin-right: 10px" id="cbFull" @change="editCheckAll">全选</el-checkbox>
                  </div>
                  <div class="delete-choose-goods" @click="delChecked">删除选中的商品</div>
                </div>
              </div>
              <div class="shipping">
                <div class="shipping-box">
                  <div class="shipping-total shipping-num"><h4
                    class="highlight">已选择 <i v-text="checkNum"></i> 件商品</h4>
                    <h5>共计 <i v-text="totalNum"></i> 件商品</h5></div>
                  <div class="shipping-total shipping-price"><h4
                    class="highlight">应付总额：<span>￥</span><i v-text="checkPrice"></i>
                  </h4>
                    <h5 class="shipping-tips ng-scope">应付总额不含运费</h5>
                  </div>
                </div>
                <el-button type="primary" :disabled="checkNum > 0 ? false : true" @click="checkout()">现在结算</el-button>
              </div>
            </div>
          </div>
        </div>
        <div v-else style="padding:50px">
          <div class="cart-e">
          </div>
          <p style="text-align: center;padding: 20px;color: #8d8d8d">你的购物车空空如也</p>
          <div style="text-align: center">
            
            <router-link to="/goods">
              <el-button type="primary">现在选购</el-button>
            </router-link>
          </div>

        </div>
      </div>
    </div>
    <MyFooter></MyFooter>
  </div>
</template>

<script>
import { mapMutations, mapState } from 'vuex'
import MyHeader from '../../common/MHeader.vue'
import MyFooter from '../../common/MFooter.vue'
import BuyNum from '../../components/BuyNum.vue'

import { getCartInfo, deleteCart, updateCart, selectAllChange } from '../../api/cart'

export default {
  name: 'Cart',
  data() {
    return {
      goods_num: 1,
      mini: 'mini',
      isFull: false,
      isabled: true,
      disable: false
    }
  },
  created() {
    this.getCartInfo()
  },
  computed: {
    ...mapState(['cartList']),
    // 选中的商品
    checkNum() {
      return this.cartList.filter(item => item.selected).reduce((total, item) => {
          return total += item.goods_number
      }, 0)
    },

    // 购物车一共有多少商品
    totalNum() {
      return this.cartList.reduce((total, item) => {
        return total += item.goods_number
      }, 0)
    },

    // 总价格
    checkPrice() {
      return this.cartList.filter(item => item.selected).reduce((total, item) => {
          return total += item.goods_number * item.goods_info.goods_price
      }, 0)
    },
  },
  methods: {
    ...mapMutations(['CARTINFO']),
    async getCartInfo() {
      let { data: res } = await getCartInfo()

      if(res.code !== 0 ) {
        return this.$notify.error({
          title: '警告',
          message: '获取购物车信息失败'
        })
      }
      this.CARTINFO(res.result.list)
      this.isFull = this.cartList.every(item => item.selected)
      this.disable = false
      this.cartList.forEach(item => {
        if(item.goods_number > item.goods_info.goods_num) {
          this.disable = true
        }
      })
      // console.log(this.cartList)
    },
    // 修改购物车商品数量
    async handleVal(num,row) {
      this.goods_num = num 

      const { data: res } = await updateCart(row.id, { number: this.goods_num })

      if(res.result.goods_number > row.goods_info.goods_num && res.result.selected === true) {
        row.selected = false
        const { data: res } = await updateCart(row.id, row)
        this.getCartInfo()
        this.isFull = this.cartList.every(item => item.selected)
      }

      if(res.code !== 0 ) {
        return this.$notify.error({
          title: '警告',
          message: '更新信息失败'
        })
      }

      this.getCartInfo()
    },
    // 修改购物车商品的选定状态
    async stateChange(cartInfo) {
      const { data: res } = await updateCart(cartInfo.id, cartInfo)

      if(res.code !== 0 ) {
        return this.$notify.error({
          title: '警告',
          message: '更新信息失败'
        })
      }

      this.getCartInfo()
      this.isFull = this.cartList.every(item => item.selected)
    },
    // 商品详情
    productDetail(id) {
      window.open('//' + window.location.host + `/#/goodsDetail/${Number(id)}`)
    },
    // 删除购物车商品
    async delGoods(id) {
      // 弹框确认用户是否删除数据
      const confirmResult = await this.$confirm('确认将这1个宝贝删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).catch(err => err)

      // 如果用户确认删除，则返回字符串 confirm,取消则是 cancel
      if (confirmResult !== 'confirm') {
        return 
      }

      const { data: res } = await deleteCart({ids: [id]})
      if(res.code !== 0 ) {
        return this.$notify.error({
          title: '警告',
          message: '删除购物车商品失败'
        })
      }
      this.getCartInfo()
    },
    // 全选/全不选
    async editCheckAll(state) {
      const { data: res } = await selectAllChange({state})

      if(res.code !== 0 ) {
        return this.$notify.error({
          title: '警告',
          message: '更新商品状态失败'
        })
      }
      this.getCartInfo()
    },
    // 删除选中的商品
    async delChecked() {
      const ids = this.cartList.filter(item => item.selected ).map(item => item.id)

      if(ids.length === 0) {
        return false
      }

      // 弹框确认用户是否删除数据
      const confirmResult = await this.$confirm(`确认将这${ids.length}个宝贝删除?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).catch(err => err)

      // 如果用户确认删除，则返回字符串 confirm,取消则是 cancel
      if (confirmResult !== 'confirm') {
        return 
      }

      const { data: res } = await deleteCart({ids})
      if(res.code !== 0 ) {
        return this.$notify.error({
          title: '警告',
          message: '删除购物车商品失败'
        })
      }
      this.getCartInfo()
    },
    checkout() {
      this.$router.push('/checkout')
    }
  },
  
  components: {
    MyHeader,
    MyFooter,
    BuyNum
  }
}
</script>

<style lang="scss" rel="stylesheet/scss" scoped>
  .store-content {
    clear: both;
    width: 1220px;
    min-height: 600px;
    padding: 0 0 25px;
    margin: 0 auto;
    .gray-box {
      position: relative;
      margin-bottom: 30px;
      overflow: hidden;
      background: #fff;
      border-radius: 8px;
      border: 1px solid #dcdcdc;
      border-color: rgba(0, 0, 0, .14);
      box-shadow: 0 3px 8px -6px rgba(0, 0, 0, .1);
      .title {
        padding-left: 30px;
        position: relative;
        z-index: 10;
        height: 60px;
        padding: 0 10px 0 24px;
        border-bottom: 1px solid #d4d4d4;
        border-radius: 8px 8px 0 0;
        box-shadow: rgba(0, 0, 0, .06) 0 1px 7px;
        background: #f3f3f3;
        background: -webkit-linear-gradient(#fbfbfb, #ececec);
        background: linear-gradient(#fbfbfb, #ececec);
        line-height: 60px;
        font-size: 18px;
        color: #333;
      }
    }
    .ui-cart {
      padding-bottom: 91px;
      .cart-table-title {
        position: relative;
        z-index: 1;
        line-height: 38px;
        height: 38px;
        padding: 0 0 0 30px;
        font-size: 12px;
        background: #eee;
        border-bottom: 1px solid #dbdbdb;
        border-bottom-color: rgba(0, 0, 0, .08);
        .name {
          float: left;
          text-align: left;
        }
        span {
          width: 137px;
          float: right;
          text-align: center;
          color: #838383;
        }
      }
      .cart-group.divide {
        .cart-items {
          border-top: 1px dashed #eee;
        }
      }
      .cart-items {
        position: relative;
        height: 140px;
        margin-left: 74px;
        /*删除*/
        .operation {
          padding: 58px 0 0;
          font-size: 12px;
          line-height: 24px;
        }
        .subtotal {
          font-weight: 700;
        }
        .item-cols-num,
        .operation,
        .price,
        .subtotal {
          overflow: hidden;
          float: right;
          width: 137px;
          text-align: center;
          color: #666;
          line-height: 140px;
        }
      }
      .cart-group.divide .cart-top-items:first-child .cart-items {
        border-top: none;
      }
      .items-choose {
        position: absolute;
        left: -74px;
        top: 0;
        width: 74px;
        height: 20px;
        padding: 60px 0 0 31px;
        font-size: 12px;
        color: #999;
      }
      .items-thumb {
        position: relative;
        margin-top: 30px;
        overflow: hidden;
        width: 80px;
        height: 80px;
      }
      img {
        width: 80px;
        height: 80px;
      }
      .cart-items .items-thumb > a, .ui-cart .cart-items .items-thumb > i {
        position: absolute;
        left: 0;
        right: 0;
        top: 0;
        bottom: 0;
        border: 1px solid #fff;
        border-radius: 3px;
        border: 0 solid rgba(255, 255, 255, .1);
        box-shadow: inset 0 0 0 1px rgba(0, 0, 0, .06);
      }
      .name {
        width: 380px;
        margin-left: 20px;
        color: #323232;
        display: table;
        a {
          color: #333;
          font-size: 16px;
        }
      }
      .name-table {
        display: table-cell;
        vertical-align: middle;
        height: 140px;
      }
      .attribute, .name p {
        color: #999;
        font-size: 12px;
        padding-top: 4px;
        line-height: 17px;
      }

    }

  }

  .page-cart {
    padding-top: 40px;
    .fix-bottom {
      height: 90px;
      width: 100%;
      position: absolute;
      bottom: 0;
      z-index: 1;
      background-position: center;
      background: #fdfdfd;
      background: -webkit-linear-gradient(#fdfdfd, #f9f9f9);
      background: linear-gradient(#fdfdfd, #f9f9f9);
      border-top: 1px solid #e9e9e9;
      box-shadow: 0 -3px 8px rgba(0, 0, 0, .04);
    }
    .cart-bar-operation {
      float: left;
      padding: 35px 26px;
      font-size: 12px;
    }
    .blue-checkbox-new {
      float: left;
      margin-right: 9px;
    }
    .choose-all, .delete-choose-goods, .selected-count {
      float: left;
      height: 20px;
      line-height: 20px;
      cursor: pointer;
      position: relative;
    }
    .delete-choose-goods {
      position: relative;
      margin-left: 21px;
      color: #bbb;
    }
    .shipping {
      float: right;
      padding: 20px 30px;
    }
    .shipping-box {
      display: inline-block;
      padding-top: 1px;
      margin-right: 10px;
    }
    .shipping-total {
      display: inline-block;
      border-left: 1px solid #e1e1e1;
      padding: 0 20px;
      .shipping-price {
        width: 155px;
        padding-right: 0;
      }
      &.shipping-num i {
        width: 28px;
        display: inline-block;
        text-align: center;
      }
      h4 {
        color: #323232;
        > i {
          font-size: 18px;
          font-weight: 700;
        }
        i, span {
          color: #d44d44;
        }

      }
      h5 {
        color: #959595;
        > i {
          font-size: 16px;
          font-weight: 700;
        }
      }

    }

    .shipping-total.shipping-num {
      text-align: right;
    }
    .shipping-total:first-child {
      border: none;
    }
    .big-main-btn {
      float: right;
      height: 48px;
    }
  }


</style>